package com.yoomiito.app.ui.my

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.adapter.my.BankAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.my.BankSearch
import com.yoomiito.app.present.my.activity.BankSearchPresenter
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.RouterUtils
import kotlinx.android.synthetic.main.act_bank_search.rv_search
import kotlinx.android.synthetic.main.act_youpin_search.*
import kotlinx.android.synthetic.main.item_search.*
import kotlinx.android.synthetic.main.item_search_toolbar.*

class BankSearchActivity : BaseActivity<BankSearchPresenter>() {
    private var mAdapter: BankAdapter? = null
    private var key: String = ""
    private val defaultSearchHint = "输入关键字"

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, BankSearchActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_bank_search

    override fun newP(): BankSearchPresenter = BankSearchPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        act_search_back.setOnClickListener { finish() }
        item_search_et.hint = defaultSearchHint
        act_search_search.setOnClickListener {
            key = item_search_et.text.toString().trim { it <= ' ' }
            if (TextUtils.isEmpty(key)) {
                val hint = item_search_et.hint.toString()
                if (defaultSearchHint == hint) {
                    showToast("请输入搜索内容")
                    return@setOnClickListener
                } else {
                    key = hint
                }
            }
            p?.getBankSearch(key)
        }

        rv_search.layoutManager = LinearLayoutManager(this)
        mAdapter = BankAdapter()
        rv_search.adapter = mAdapter
        mAdapter?.setOnItemClickListener(
            BaseQuickAdapter.OnItemClickListener { adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int ->
                val intent = Intent()
                intent.putExtra("bankId", mAdapter?.data?.get(position)?.id ?: 0)
                intent.putExtra("bankName", mAdapter?.data?.get(position)?.bank_name ?: "")
                setResult(0, intent)
                finish()


            }
        )
        p?.getBankSearch(key)

    }


    fun setData(data: List<BankSearch>) {
        mAdapter?.setNewData(data)
    }

    private fun toResult() {
    }


}